var app = getApp();
Page({
  data: {
    section: [{
      name: '全部',
      typeId: '1'
    }, {
      name: '待付款',
      typeId: '2'
    }, {
      name: '待使用',
      typeId: '3'
    },
    {
      name: '已完成',
      typeId: '4'
    }],
    currentId: '1',
    page: 1,
    no_more: false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var type = options.type;
    var store_id = wx.getStorageSync("store_id");
    this.setData({
      type:type
    })
    if(!store_id){
      wx.navigateTo({
        url: '/pages/index/index'
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var store_id = wx.getStorageSync("store_id");
    if(!store_id){
      wx.navigateTo({
        url: '/pages/index/index'
      });
    }else{
      // 获取订单列表
      this.getOrderList();
    }
  },

  /**
   * 获取订单列表
   */
  getOrderList: function () {
    let _this = this;
    var page = _this.data.page;
    var store_id = wx.getStorageSync("store_id");
    app.acc5ShowLoading(_this)
    app.visit('account/orderLists?page='+page, {
      type:_this.data.currentId,
      leixing:_this.data.type,
      store_id:store_id
    }, '',function (result) {
      var lls = result.data;
      console.log(lls)
      if (lls.length < 10) {
        _this.setData({
          no_more: true
        })
      }
      app.acc5HideLoading(_this)
      console.log("订单列表", result);
      if (page == 1) {
        _this.setData({
          list: lls,
          page: page + 1
        })
      } else {
        var list = _this.data.list.concat(lls)
        _this.setData({
          list: list,
          page: page + 1
        })
      }
    });
  },

  /**
   * 下拉到底加载数据
   */
  onReachBottom: function () {
    // 已经是最后一页
    if (this.data.no_more == true) {
      return false;
    } else {
      this.getOrderList();
    }
  },

  handleTap: function (e) {
    let id = e.currentTarget.id;
    console.log(id)
    this.setData({
      currentId: id,
      page:1,
      no_more: false,
    })
    this.getOrderList();
  },
  pj: function (e) {
    let order_id = e.currentTarget.dataset.order_id;
    wx.navigateTo({
      url: "pj?order_id="+order_id
    });
  },
  qxdd:function(e){
    let _this = this;
    let order_id = e.currentTarget.dataset.order_id;
    var user_id = wx.getStorageSync("user_id");
    wx.showModal({
      title: "提示",
      content: "确认取消订单？",
      success: function (o) {
        if (o.confirm) {
          app.visit('drinker/cancel', {
            order_id:order_id,user_id:user_id
          }, '',function (result) {
            _this.getOrderList();
          });
        }
      }
    });
  },
  fukuan:function(e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.order_id;
    var user_id = wx.getStorageSync("user_id");
    wx.showModal({
      title: "提示",
      content: "确认付款吗？",
      success: function (o) {
        if (o.confirm) {
          app.visit('drinker/payAgainGoodOrder', {
            user_id: user_id,
            order_id:order_id
          }, 'POST',function (result) {
            console.error('支付',result);
            if(result.code == 0){
              // 发起微信支付
              wx.requestPayment({
                timeStamp: result.data.payTimeStamp,
                nonceStr: result.data.paynonceStr,
                package: result.data.payPackage,
                signType: 'RSA',
                paySign: result.data.paySign,
                success: function (res) {
                  wx.showToast({
                    title: '支付成功',
                    icon: 'none',
                    success: function () {
                       _this.getOrderList();
                    }
                  })
                }
              });
            }else{
              app.myToast('调取支付失败')
            }
            wx.hideLoading();
          });
        }
      }
    });
  },
  qt_detail: function (e) {
    var order_id = e.currentTarget.dataset.order_id
    wx.navigateTo({
      url: 'qt_detail?order_id='+order_id
    });
  },
    shangjia:function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../new/shangjia?id="+id
        });
    }
})